#ifndef _H_UTILS
#define _H_UTILS

typedef uint8_t boolean;

/*
 * ================== Global Prototypes
 */

// From Maxim 11068 guide.
uint8_t crc8(uint8_t* data, uint8_t len, uint8_t crc = 0);

// to read a command from Serial.
boolean readCmd(char* buf, int bufSize);

// represent a number as a 2-byte string.
void hex2str(uint8_t num, char* buf);

// verify string passed in hex digit.
#define isHexChar(c) ((c>='0'&&c<='9')||(c>='A' && c<='F'))

// convert a string to a hex number. NO CHECKING DONE.
void str2hex(char* buf, uint8_t& num);


/*
 * ================== Helper Classes
 */

/*
 * ================== Class Definitions
 */
// calculate crc as data is added.
class Pec
{
  uint8_t crc;

  public:
  Pec() {crc=0;}
  void reset() {crc=0;}
  void add(uint8_t d) {
    crc = crc8(&d,1,crc);
  }
  void add(uint8_t* d, uint8_t len) {
    crc = crc8(d,len,crc);
  }

  uint8_t getCRCreset() {
    uint8_t ret = crc;
    crc = 0;
    return ret;
  }
  uint8_t getCRC() {return crc;}
  boolean verify(uint8_t crcIn) {return (crc == crcIn);}
};


#endif
